package models

import (
	"base_srv/basic/config"
	"time"
)

type Application struct {
	Id              uint32    `gorm:"column:id;type:int UNSIGNED;comment:    ;primaryKey;not null;" json:"id"`                          //
	AppName         string    `gorm:"column:app_name;type:varchar(30);comment:应用名称;not null;" json:"app_name"`                          // 应用名称
	AppSortOrder    uint32    `gorm:"column:app_sort_order;type:int UNSIGNED;comment:应用排列顺序;not null;" json:"app_sort_order"`           // 应用排列顺序
	AppIcon         string    `gorm:"column:app_icon;type:varchar(255);comment:应用图标;not null;" json:"app_icon"`                         // 应用图标
	AppPackageName  string    `gorm:"column:app_package_name;type:varchar(50);comment:应用包名;not null;" json:"app_package_name"`          // 应用包名
	AppStatus       int8      `gorm:"column:app_status;type:tinyint(1);comment:应用状态 1-待启用  2-启用;not null;default:1;" json:"app_status"` // 应用状态 1-待启用  2-启用
	UserId          uint32    `gorm:"column:user_id;type:int UNSIGNED;comment:用户id;not null;" json:"user_id"`                           // 用户id
	UploadTime      time.Time `gorm:"column:upload_time;type:datetime;comment:上传时间;not null;" json:"upload_time"`                       // 上传时间
	StartUpdateTime time.Time `gorm:"column:start_update_time;type:datetime;comment:开始更新时间;not null;" json:"start_update_time"`         // 开始更新时间
	CreateAt        time.Time `gorm:"column:create_at;type:datetime;not null;default:CURRENT_TIMESTAMP;" json:"create_at"`
	UpdateAt        time.Time `gorm:"column:update_at;type:datetime;not null;default:CURRENT_TIMESTAMP;" json:"update_at"`
	DeleteAt        time.Time `gorm:"column:delete_at;type:datetime;default:NULL;" json:"delete_at"`
}

func (a *Application) TableName() string {
	return "application"
}

// 添加应用
func (a *Application) Create() error {
	return config.DB.Create(&a).Error
}

// 修改应用
func (a *Application) Update() error {
	return config.DB.Debug().Updates(&a).Error
}

// 应用添加记录列表
func (a *Application) List() ([]ApplicationResp, error) {
	var list []ApplicationResp
	err := config.DB.Model(&Application{}).Select("application.id, application.app_name, application.app_sort_order, application.app_icon," +
		"application.app_package_name, application.app_status, application.user_id, application.upload_time, application.start_update_time,user.real_name").
		Joins("join user on application.user_id = user.id").
		Where("application.delete_at is null").Find(&list).Error
	return list, err
}

type ApplicationResp struct {
	Id              uint32    `json:"id"`
	AppName         string    `json:"appName"`
	AppSortOrder    uint32    `json:"appSortOrder"`
	AppIcon         string    `json:"appIcon"`
	AppPackageName  string    `json:"appPackageName"`
	AppStatus       int8      `json:"appStatus"`
	RealName        string    `json:"realName"`
	UploadTime      time.Time `json:"uploadTime"`
	StartUpdateTime time.Time `json:"startUpdateTime"`
}

// 应用删除
func (a *Application) Delete() error {
	return config.DB.Delete(&a).Error
}
